import SceneCom from "./SceneCom";
import SceneManager from "./SceneManager";
import GameManager from "../common/GameManager";
import Utils from "../common/Util";
import PopUpManager from "../common/PopUpManager";
import LangManager from "../i18n/LangManager";
import AudioManager from "../common/AudioManager";
import { GameConfig } from "../common/GameConfig";
import UserData from "../common/UserData";
import Facebook from "../Facebook/Facebook";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StartScene extends SceneCom {

    @property(cc.Node)
    startBtn: cc.Node = null;

    @property(cc.RichText)
    histroyHighestScore: cc.RichText = null;

    @property(cc.Label)
    version: cc.Label = null;

    @property(cc.Node)
    rankBtn: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    
    start() {
        GameManager.getInstance().gameData.ReadGameArchive();
        //设置声音
        GameConfig.SoundSwitch = GameManager.getInstance().gameData.getSoundSwitch();
        //背景音乐
        GameManager.getInstance().audioManager.playBackgroundMusic();

        UserData.init();
    }

    onShow() {
        super.onShow();
        this.histroyHighestScore.string = `<b>${LangManager.getStrByKey("highest_score") + GameManager.getInstance().gameData.ReadHighestScore()}</b>`;

        //开始游戏按钮
        this.startBtn.on(cc.Node.EventType.TOUCH_START, this.startBtnClicked, this);

        if(window["version"]){
            this.version.string = window["version"];
        }

        if (typeof gmbox != 'undefined') {
            gmbox.loadingComplete();
        }

        this.rankBtn.on(cc.Node.EventType.TOUCH_START, this.openRankPage, this);
    }

    onHide() {
        super.onHide();
        this.startBtn.on(cc.Node.EventType.TOUCH_START, this.startBtnClicked, this);
    }

    private startBtnClicked() {
        let audioManager: AudioManager = GameManager.getInstance().audioManager;
        audioManager.PlayBtnSound();

        this.startGame();
    }

    private startGame() {
        GameManager.getInstance().sceneManager.switchScene(SceneManager.SceneType.GameScene, SceneManager.SceneSwitchType.RightToLeft);
    }
    
    /**
     * 打开排行榜
     */
    private _rulePage:cc.Node;
    private openRankPage() {
        // Facebook.FBRank.GetGlobalRank();
        Utils.GetPrefab("prefab/VerScrollview.prefab", (prefab) => {
            if (prefab && (!this._rulePage || !this._rulePage.parent)) {
                this._rulePage = cc.instantiate(prefab);
                this.node.addChild(this._rulePage);
            }
        }, this)
    }

    // update (dt) {}
}
